package com.tangfuzhong.ext;

import java.util.Arrays;

import com.tangfuzhong.bean.Fish;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		int count = beanFactory.getBeanDefinitionCount();
		String[] names = beanFactory.getBeanDefinitionNames();
		System.out.println("MyBeanFactoryPostProcessor-postProcessBeanFactory当前BeanFactory中有"+count+"个Bean：");
		System.out.println(Arrays.asList(names));

		//改变beanDefinition的信息
		BeanDefinition beanDefinition = beanFactory.getBeanDefinition("fish");
		MutablePropertyValues pv =  beanDefinition.getPropertyValues();
		pv.addPropertyValue("price", 9999);
		pv.addPropertyValue("name", "beautiful fish");
		beanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);

	}

	@Bean
	public Fish fish()
	{
		Fish fish = new Fish();
		fish.setType(1);
		fish.setName("marry fish");
		fish.setFrom("珠三角");
		fish.setPrice(16);
		return fish;
	}

}
